package com.yanqu.road.server.cmd.banquet.party;

import com.yanqu.road.entity.banquet.BanquetInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.banquet.BanquetModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.banquet.BanquetMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = GameProtocol.S_CROSS_PARTY_OPEN_RESULT, desc = "")
public class CrossOpenPartyResultCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        BanquetProto.CrossOpenPartyRespMsg msg = BanquetProto.CrossOpenPartyRespMsg.parseFrom(packet.getBytes());
        ret = msg.getRet();
        int type = msg.getType();
        String invitationId = msg.getInvitationId();
        BanquetInfo banquetInfo = BanquetConfigMgr.getBanquetInfo(type);
        String consume = "";
        if (!"0".equals(banquetInfo.getOpenConsume())) {
            consume = banquetInfo.getOpenConsume();
        }
        if (!StringUtils.isNullOrEmpty(consume)) {
            player.getModule(PreReduceModule.class).restorePreReduce(consume);
        }
        if (ret != 0) {
            //开启失败 返回帖子
            player.getModule(BanquetModule.class).resetInvitation(invitationId);
        }else {
            if(!player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(banquetInfo.getOpenConsume()),
                    eLogMoneyType.Banquet,eLogMoneyType.BanquetOpenConsume)){
                ret =  GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                return;
            }
            //开启成功 扣道具
            BanquetMgr.addBanquetConductTimes(player);
            BanquetMgr.syncSelfBanquet(player, null);
            player.getModule(BanquetModule.class).updateUserBanquetSettingBanquetId(type, msg.getBanquetId());
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        BanquetProto.OpenBanquetResp.Builder respMsg = BanquetProto.OpenBanquetResp.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(Protocol.U_BANQUET_SERVER_OPEN, respMsg);
    }
}
